ebaycategory = {}   

// - Start
ebaycategory.listCat;//Khai báo var
//Print category Option for dropdown box
ebaycategory.level = 0;

//Print List NewsCat
ebaycategory.printCatList = function(parentId){
    $.each(ebaycategory.listCat, function(){
        var map = this;        
            
        if(map.parentId == parentId){
            ebaycategory.level ++;
            var tmp = "";
            var i = ebaycategory.level;
            while(i){
                tmp += "- - ";  
                i--;
            }                                   
            $('table').append('<tr level="'+ebaycategory.level+'" align="center" for="'+map.categoryId+'">\
                <td><p>'+map.categoryId+'</p></td>\
                <td style="padding-left:50px;text-align:left"><p>'+tmp+map.name+'</p></td>\
                <td><p><input type="text" value="'+map.ebayCatId+'" size="40" for="'+map.categoryId+'" class="ebayCatId" /></p></td>\
                <td><p><input type="text" value="'+map.priceMultiple+'" size="5" for="'+map.categoryId+'" class="priceMultiple" /></p></td>\
                <td><p><input type="text" value="'+map.priceAddition+'" size="5" for="'+map.categoryId+'" class="priceAddition" /></p></td>\
            </tr>');
            ebaycategory.printCatList(map.categoryId);
            ebaycategory.level --;
        }
    })
}
// - End

ebaycategory.list = function(){
    main.init();
    fly.ajax({   
        service:'/ebayCategory/list',
        success: function(result){
            if(result.status){
                ebaycategory.listCat = result.data;
                $('#content').html(fly.template('http://naima.vn/ebaycategory/main.html', {
                    //count_data: result.data.length
                }));   
                
                $('#list-checkall').click(function(){
                    $('input[type=checkbox][for]').attr('checked', $('#list-checkall').is(':checked'))
                });
                $('#total-count').text(result.data.length);
                ebaycategory.printCatList(0);                
            }
            else{
                popup.msg(result.message);
            }
        }
    });
}

ebaycategory.editEbayMap = function(categoryId, level){
    fly.ajax({
        service: '/ebayCategory/get',
        data: {categoryId:categoryId},
        success: function(result){
            popup.open('popup-ebay-category-form', 'Map danh mục ebay', fly.template('http://naima.vn/ebaycategory/map.html', {
                data: result.data
            }), [
            {
                title: 'Cập nhật',
                fn: function(){
                    fly.submit({
                        id: 'ebay-category-form',
                        service: '/ebayCategory/editEbayMap',
                        success: function(rs){
                            var tmp = "";
                            var i = level;
                            while(i){
                                tmp += "- - ";
                                i--;
                            }  
                            $('.table tr[for='+rs.data.categoryId+']').replaceWith(fly.template('http://naima.vn/ebaycategory/row.html',{
                                data: rs.data, 
                                lvl: level,
                                tmp: tmp,
                            }));
                            popup.close('popup-ebay-category-form');
                        }
                    });
                }
            },
            {
                title: 'Hủy bỏ',
                fn:function(){
                    popup.close('popup-ebay-category-form');
                }
                    
            }
            ]);    
        }
    });
}

ebaycategory.saveChange = function(){
    
    var data = Array();    
    var i = 0;

    $('.table tr[for]').each(function(){
        var ebayCate = new Object();          
        ebayCate.id = parseInt($(this).attr('for'));  
        ebayCate.ebayCatId = $('input[type="text"][class="ebayCatId"][for="'+$(this).attr('for')+'"]').attr('value');
        ebayCate.priceAddition = parseFloat($('input[type="text"][class="priceAddition"][for="'+$(this).attr('for')+'"]').attr('value'));
        ebayCate.priceMultiple = parseFloat($('input[type="text"][class="priceMultiple"][for="'+$(this).attr('for')+'"]').attr('value')); 
        data[i] = ebayCate;
        i++;
    });
    
    popup.confirm("Bạn có chắc chắn muốn lưu các thay đổi ?", function(){
        fly.ajax({
            service: '/ebayCategory/saveChange',
            method: 'post',
            data:{
                ebayCategories:JSON.stringify(data)
            },
            success: function(result){
                popup.msg('Cập nhật thay đổi thành công !');                 
            }
        });
    });

} 